/*
  1.获取类别查询参数
    -类别  ID  待会根据ID查询商品
	-类别名称  渲染菜单
 */

/* 分页 */
let page = {
	currentPage: 1, //当前页
	pageSize: 9, //页码大小
	count: 0, //数据总数
	goodsList: [], //当前页商品列表
	total: 0, //总页数
	search: '', //搜索条件
	cat_id: '', //类别ID
	search_type: 1, //1.根据关键词查询  2.根据类别查询
	pageList: [9, 18, 27] //页码大小修改
}

/* 定义数据渲染函数 */
function render(currentPage, pageSize, searchType, value, resolve) {
	
	
	//参数字符串
	let params = 'page=' + currentPage + "&pagesize=" + pageSize;
	let address = '';
	if (searchType == 2) {
		params += "&catId=" + value;
		address = '/api_goods';
	} else {
		params += "&keywords=" + value;
		address = '/api_search';
	}
	ajax({
		method: 'GET',
		type: 'JSON',
		url: "http://110.41.43.118:3008" + address + "?" + params,
		callback: function(data) {
			let goodList = data.data;
			page.goodsList = goodList;
			page.count = data.count;
			page.total = data.page;
			if(page.total == undefined){
				page.total =  page.count % page.pageSize == 0 ? page.count / page.pageSize : Math.ceil(page.count / page.pageSize);
			}
			//处理细节
			if(page.currentPage > page.total){
				page.currentPage = page.total;
				//重新查询数据
				render(page.currentPage, pageSize, searchType, value, resolve)
			}

			let hotUL = document.querySelector(".hot>ul");
			let text = '';
			console.log('开始渲染商品')
			page.goodsList.forEach(item => {
				text += "<li>" +
					"<div class='hot_img'>" +
					"<img src='" + item.goods_thumb + "'/>" +
					"<p>良仓双12</p>" +
					"<img src='img/double12.jpg'/>" +
					"</div>" +
					"<div class='good_info'>" +
					"<p>" +
					"<span>" + item.goods_name + "</span><br>" + item.goods_desc +
					"</p>" +
					"</div>" +
					"<div class='good_other'>" +
					"<div>" +
					"<img src='" + item.brand_thumb + "'/>" +
					"<span>" + item.brand_name + "</span>" +
					"<span class='nice'>" + item.star_number + "</span>" +
					"</div>" +
					"</div>" +
					"</li>"
			})
			hotUL.innerHTML = text;
			resolve();
		}
	})
}

/*
 * currentPage:当前页面
 * total：页面总数
 * */
function pageScope(currentPage, total) {
	let pageScope = [];
	//页面总数小于等于10
	let startPage = 1;
	let endPage = total;
	if (total > 10) {
		startPage = currentPage - 5;
		endPage = currentPage + 4;
		//首页溢出判断
		if (startPage <= 0) {
			startPage = 1;
			endPage = 10;
		}
		//尾页溢出
		if (endPage > total) {
			startPage = total - 9;
			endPage = total;
		}
	}

	for (let i = startPage; i <= endPage; i++) {
		pageScope.push(i);
	}
	return pageScope;
}


/* 
 * 渲染分页
 *current:当前页
 *count:商品总数 
 */
function renderPage(currentPage, pageSize, total) {
	console.log(currentPage,total)
	//获得页码数组
	let pages = pageScope(currentPage, total);
	console.log(pages);
	//获取渲染列表对象
	let list = document.querySelector(".page_list>ul");
	list.innerHTML = '';
	console.log('开始渲染页码')
	if (pages.length > 0) {

		list.innerHTML += "<li><a href='#' class='cicle' onclick='previous()'><</a></li>";
		for (let value of pages) {
			if (value == currentPage) {
				list.innerHTML += "<li><a href='#' class='active' onclick='changePageNo(" + value + ")'>" + value +
					"</a></li>";
			} else {
				list.innerHTML += "<li><a href='#' class='cicle' onclick='changePageNo(" + value + ")'>" + value +
					"</a></li>";
			}
		}
		list.innerHTML += "<li><a href='#' class='cicle' onclick='next()'>></a></li>";
	}

	let pageInfo = document.getElementById("pageInfo");
	pageInfo.innerHTML = currentPage + "/" + total;

	if (pages.length == 0) {
		let pageChange = document.querySelector(".to_page");
		pageChange.style.display = "none";
	} else {
		let pageChange = document.querySelector(".to_page");
		pageChange.style.display = "inline-block";
	}
}

function changePageNo(pageNo) {
	page.currentPage = pageNo;
	let value = '';
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	//重新查询重新渲染数据
	new Promise((resolve, reject) => {
		//渲染商品数据
	
		render(page.currentPage, page.pageSize, page.search_type, value, resolve);
		//resolve();
		//表示函数执行成功
	}).then(() => {
		//必须是上一个函数执行成功，下一个函数才会执行
		//渲染页码数据
		renderPage(page.currentPage, page.pageSize, page.total);
	})
}

function previous() {
	let pageNo = page.currentPage - 1;
	if (pageNo <= 0) {
		return;
	}
	page.currentPage = pageNo;
	let value = '';
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	//重新查询重新渲染数据
	new Promise((resolve, reject) => {
		//渲染商品数据
	
		render(page.currentPage, page.pageSize, page.search_type, value, resolve);
		//resolve();
		//表示函数执行成功
	}).then(() => {
		//必须是上一个函数执行成功，下一个函数才会执行
		//渲染页码数据
		renderPage(page.currentPage, page.pageSize, page.total);
	})
}

function next() {
	let pageNo = page.currentPage + 1;
	if (pageNo > page.total) {
		return;
	}
	page.currentPage = pageNo;
	let value = '';
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	//重新查询重新渲染数据
	new Promise((resolve, reject) => {
		//渲染商品数据
	
		render(page.currentPage, page.pageSize, page.search_type, value, resolve);
		//resolve();
		//表示函数执行成功
	}).then(() => {
		//必须是上一个函数执行成功，下一个函数才会执行
		//渲染页码数据
		renderPage(page.currentPage, page.pageSize, page.total);
	})
}


function changePageSize(){
	let pageSize = document.querySelector("select[name='pageSize']").value;
	page.pageSize = pageSize;
	
	//重新查询重新渲染数据
	let value = '';
	if(page.search_type == 2){
		value = page.cat_id;
	}else{
		value = page.search;
	}
	//重新查询重新渲染数据
	new Promise((resolve, reject) => {
		//渲染商品数据
	
		render(page.currentPage, page.pageSize, page.search_type, value, resolve);

		
		//resolve();
		//表示函数执行成功
	}).then(() => {
		//必须是上一个函数执行成功，下一个函数才会执行
		//渲染页码数据
		renderPage(page.currentPage, page.pageSize, page.total);
	})
}

function toPageNo(){
	//修改页码添加监听监听
	let page_btn = document.querySelector("button[class='page_btn']");
	page_btn.addEventListener("click",()=>{
		let pageNo = document.querySelector("input[name='pageNo']").value;
		console.log(pageNo);
		if(pageNo == ''){
			alert('请输入页码');
			return;
		}
		if(pageNo <= 0 || pageNo > page.total){
			alert('页码错误');
			return;
		}
		page.currentPage = pageNo;
		let value = '';
		if(page.search_type == 2){
			value = page.cat_id;
		}else{
			value = page.search;
		}
		//重新查询重新渲染数据
		new Promise((resolve, reject) => {
			//渲染商品数据
		
			render(page.currentPage, page.pageSize, page.search_type, value, resolve);
			//resolve();
			//表示函数执行成功
		}).then(() => {
			//必须是上一个函数执行成功，下一个函数才会执行
			//渲染页码数据
			renderPage(page.currentPage, page.pageSize, page.total);
		})
	})
}



/* 页码加载初始化数据 */
window.onload = function() {
	let searchParams = new URLSearchParams(window.location.search);
	//获取查询类型
	let search_type = searchParams.get('search_type');
	if (search_type != undefined || search_type != '') {
		page.search_type = search_type;
	}

	if (page.search_type == 2) {
		let cat_name = searchParams.get('cat_name');
		let cat_id = searchParams.get('cat_id');

		//渲染类别
		let type = document.querySelector("span[class='type']");
		type.innerHTML = ">" + cat_name;
		//渲染商品列表
		page.cat_id = cat_id;


		new Promise((resolve, reject) => {
			//渲染商品数据

			render(page.currentPage, page.pageSize, page.search_type, cat_id, resolve);
			//resolve();
			//表示函数执行成功
		}).then(() => {
			//必须是上一个函数执行成功，下一个函数才会执行
			//渲染页码数据
			renderPage(page.currentPage, page.pageSize, page.total);
		})



	} else {
		let keywords = searchParams.get('keywords');
		new Promise((resolve, reject) => {
			//渲染商品数据
			render(page.currentPage, page.pageSize, page.search_type, keywords,resolve);
		}).then(()=>{
			//渲染页码数据
			renderPage(page.currentPage, page.pageSize, page.total);
		})
		
		
	}

	/* 初始化页码大小 */
	let page_select = document.querySelector("select[class='page_size']");
	let text = ''
	page.pageList.forEach((item) => {
		text += "<option>" + item + "</option>"
	});
	page_select.innerHTML = text;
	
	/* 渲染类别 */
	(function(){
		ajax({
			method:'GET',
			type:'JSON',
			url:BASE_URL+'/api_cat',
			callback:function(data){
				//获取渲染的父模块
				let typeList = document.querySelector(".typeList>ul");
				let goodListData = data.data;
				goodListData.forEach((item, index) => {
					let li = document.createElement("li");
					li.innerHTML = "<a href='goodlist.html?search_type=2&cat_id="+item.cat_id+"&cat_name="+item.cat_name+"'>"+item.cat_name+"</a>";
					typeList.appendChild(li);
				});
			}
		})
	})();
}